#include "GridGround.h"

/// <summary>
/// Default Constructor
/// </summary>
CIView::GridGround::GridGround()
: edgeLength (80.0f), parts(40) 
{}

/// <summary>
/// Constructor
/// </summary>
/// <param name="_edgeLength"> The ground edge length value </param>
CIView::GridGround::GridGround (int _edgeLength)
: edgeLength (_edgeLength), parts(20)  
{}

/// <summary>
/// Destructor
/// </summary>
CIView::GridGround::~GridGround() {}






///<************************************************************************************************************>
///<**************************************** region Utility Methods ********************************************>
///<************************************************************************************************************>

/// <summary>
/// Method called whenever the ground has to be rendered
/// </summary>
void CIView::GridGround::Render() {
    glLineWidth (1.0f);

    glPushMatrix();
        glColor3f (0.2f, 0.2f, 0.2f);                            //Defines the ground's color

        ////Defining ground's material
        //float matamb[4], matdif[4], matspe[4];

        //matamb[0]=0.05; matamb[1]=0.05; matamb[2]=0.2; matamb[3]=1;
        //matdif[0]=0.2; matdif[1]=0.2; matdif[2]=0.8; matdif[3]=1;
        //matspe[0]=0.0; matspe[1]=0.0; matspe[2]=0.0; matspe[3]=1;

        //glMaterialfv (GL_FRONT_AND_BACK, GL_AMBIENT, matamb);
        //glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, matdif);
        //glMaterialfv (GL_FRONT_AND_BACK, GL_SPECULAR, matspe);
        //glMaterialf (GL_FRONT_AND_BACK, GL_SHININESS, 50.0);

        //Multiplying the current matrix at the current-matrix-stack top by a translation matrixes        
        glTranslatef (-edgeLength/2, 0, -edgeLength/2);      //Translates the ground
        
        glNormal3f (0, 1, 0);                               //Defines the ground's normal

        //Dividing the ground in parts^2 squares
        int segment = (int)(edgeLength / parts);

        for (int i = 0; i < parts; i++)                     //Loops to draw the ground divided in parts^2 squares
            for (int j = 0; j < parts; j++) {
                glBegin (GL_LINE_LOOP);
                    glVertex3f (i*segment, 0, j*segment);
                    glVertex3f (i*segment, 0, (j+1)*segment);
                    glVertex3f ((i+1)*segment, 0, (j+1)*segment);
                    glVertex3f ((i+1)*segment, 0, j*segment);
                glEnd();
            }
    glPopMatrix();
}

/// <summary>
/// Setter Method to set the ground edge length
/// </summary>
/// <param name="_edgeLength"> The edge length </param>
void CIView::GridGround::SetEdgeLength (int _edgeLength) {
    edgeLength = _edgeLength;
}

/// <summary>
/// Setter Method to set the number of parts the ground's edge has to be divided
/// </summary>
/// <param name="p"> The number of parts the ground's edge has to be divided </param>
void CIView::GridGround::SetEdgePartition (int p) {
    parts = p;
}
/***************************************************************************************************************
******************************************* endregion Utility Methods ******************************************
****************************************************************************************************************/
